package com.exam.service.impl;

import com.exam.mapper.LoginUserMapper;
import com.exam.mapper.TeacherMapper;
import com.exam.pojo.LoginUser;
import com.exam.pojo.Student;
import com.exam.pojo.Teacher;
import com.exam.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private LoginUserMapper loginUserMapper;
    @Override
    public Teacher queryById(Integer teacherId) {
        return teacherMapper.queryById(teacherId);
    }

    @Override
    public PageInfo<Teacher> queryAllPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> teachers = teacherMapper.queryAll();
        PageInfo<Teacher> pageInfo = new PageInfo<>(teachers);
        return pageInfo;
    }

    @Override
    public int updateById(Teacher teacher) {
        return teacherMapper.updateById(teacher);
    }

    @Override
    public Boolean deleteByUserId(Integer userId) {
        int count = loginUserMapper.deleteById(userId);
        if (count == 0) return false;
        return true;
    }

    @Override
    public List<Integer> queryTeaId() {
        return teacherMapper.queryTeaIdAll();
    }

    @Override
    @Transactional
    public void insert(Teacher teacher) {
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(String.valueOf(teacher.getTeacherId()));
        loginUser.setPassword("123456");
        loginUser.setRole("1");
        int count = loginUserMapper.insert(loginUser);
        if (count == 0) throw new RuntimeException("登录信息添加失败");
        teacher.setUserId(loginUser.getId());
        int insert = teacherMapper.insert(teacher);
        if (insert == 0) throw new RuntimeException("教师信息添加失败");
    }
}
